package at.jku.ssw.fortress.builder;

import java.util.Map;

import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;

public class FortressBuilder extends IncrementalProjectBuilder {
	
	public static final String BUILDER_ID = "at.jku.ssw.fortress.builder";

	protected IProject[] build(int kind, Map args, IProgressMonitor monitor)
			throws CoreException {
		if (kind == FULL_BUILD) {
			fullBuild(monitor);
		} else {
			IResourceDelta delta = getDelta(getProject());
			if (delta == null) {
				fullBuild(monitor);
			} else {
				incrementalBuild(delta, monitor);
			}
		}
		return null;
	}

	/**
	 * Performs a full build.
	 * 
	 * @param monitor
	 *            the progress monitor.
	 * @throws CoreException
	 */
	protected void fullBuild(IProgressMonitor monitor) throws CoreException {
		getProject().accept(new FortressBuildVisitor());
	}

	/**
	 * Performs a incremental build.
	 * 
	 * @param delta
	 *            the resource delta.
	 * @param monitor
	 *            the progress monitor.
	 * @throws CoreException
	 */
	protected void incrementalBuild(IResourceDelta delta,
			IProgressMonitor monitor) throws CoreException {
		delta.accept(new FortressBuildVisitor());
	}

	/**
	 * Appends this builder to the given target project.
	 * 
	 * @param project
	 *            the target project.
	 * @throws CoreException
	 */
	public static void appendTo(IProject project) throws CoreException {
		Assert.isNotNull(project);
		IProjectDescription desc = project.getDescription();
		ICommand[] commands = desc.getBuildSpec();
		boolean found = false;

		for (ICommand command : commands) {
			if (command.getBuilderName().equals(BUILDER_ID)) {
				found = true;
				break;
			}
		}
		if (!found) {
			// add builder to project
			ICommand command = desc.newCommand();
			command.setBuilderName(BUILDER_ID);
			ICommand[] newCommands = new ICommand[commands.length + 1];
			// Add it before other builders.
			System.arraycopy(commands, 0, newCommands, 1, commands.length);
			newCommands[0] = command;
			desc.setBuildSpec(newCommands);
			project.setDescription(desc, null);
		}
	}

	/**
	 * Removes this builder from the given target project.
	 * 
	 * @param project
	 *            the target project.
	 * @throws CoreException
	 */
	public static void removeFrom(IProject project) throws CoreException {
		IProjectDescription desc = project.getDescription();
		ICommand[] commands = desc.getBuildSpec();

		for (int i = 0; i < commands.length; ++i) {
			if (commands[i].getBuilderName()
					.equals(BUILDER_ID)) {
				ICommand[] newCommands = new ICommand[commands.length - 1];
				System.arraycopy(commands, 0, newCommands, 0, i);
				System.arraycopy(commands, i + 1, newCommands, i,
						commands.length - i - 1);
				desc.setBuildSpec(newCommands);
				project.setDescription(desc, null);
				break;
			}
		}
	}
}
